//入口函数
$(function () {
  getUserInfo();
  // 退出登录
  $("#btnLogout").on("click", function (e) {
    // alert(1);
    layer.confirm(
      "确定退出登录?",
      { icon: 3, title: "提示" },
      function (index) {
        //do something
        localStorage.clear();
        location.href = "/login.html";
        layer.close(index);
      }
    );
  });
});
function getUserInfo() {
  $.ajax({
    method: "get",
    url: "/my/userinfo",
    success: function (res) {
      console.log(res);
      if (res.status !== 0) {
        return layer.msg(res.message);
      }
      //获取到显示用户信息
      renderAvatar(res.data);
    },
  });
}
function renderAvatar(user) {
  var name = user.nickname || user.username;
  $("#welcome").html("欢迎&nbsp;" + name);
  //填充用户头像
  if (user.user_pic) {
    $(".layui-nav-img").attr("src", user.user_pic).show();
    $(".text-avatar").hide();
  } else {
    $(".layui-nav-img").hide();
    $(".text-avatar").html(name[0].toUpperCase()).show();
  }
}
